<?php

include_once 'DBCon.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RedeemItem
 *
 * @author Kenny
 */
class RedeemItem {

    private $itemcode;
    private $itemname;
    private $pointrequired;
    private $value;
    private $status;
    private $itemtype;
    private $couponno;
    private $redeemdate;
    private $getredeemableitem = 'SELECT * FROM redeemableitem;';
    private $getfiltereditem = 'SELECT * FROM redeemableitem WHERE itemType = ?;';
    private $redeemitem = 'INSERT INTO redeemeditem(referenceNo, icNo, itemCode, redeemDate, status) VALUES(?,?,?,NOW(), "UNUSED");';
    private $getitemvalue = 'SELECT value FROM redeemableitem WHERE itemCode = ?;';
    private $checkcoupon = 'SELECT referenceNo, value, r.itemCode, status FROM redeemableitem r, redeemeditem ri WHERE ri.itemCode = r.itemCode AND referenceNo =?;';
    private $getredeemhistory = 'SELECT referenceNo, itemCode, redeemDate, status FROM redeemeditem WHERE icNo = ? ORDER BY redeemDate DESC;';
    private $updatecouponstatus = 'UPDATE redeemeditem SET status = "USED" WHERE referenceNo = ?;';
    private $con;

    function __construct() {
        $this->con = DBCon::connectDB();
    }

    public function getAllRedeemableItem() {
        $result = array();
        $data = null;
        $pstmtselect = $this->con->prepare($this->getredeemableitem);
        $pstmtselect->execute();
        $pstmtselect->bind_result($result['itemcode'], $result['itemname'], $result['pointrequired'], $result['value'], $result['itemtype'], $result['itemimage']);
        while ($pstmtselect->fetch()) {
            foreach ($result as $key => $value) {
                $row[$key] = $value;
            }
            $data[] = $row;
        }
        if ($data != null) {
            return $data;
        } else {
            return null;
        }
    }

    public function getFilteredItem() {
        $result = array();
        $data = null;
        $itemtype = trim($_POST['type']);
        $pstmtselect = $this->con->prepare($this->getfiltereditem);
        $pstmtselect->bind_param('s', $itemtype);
        $pstmtselect->execute();
        $pstmtselect->bind_result($result['itemcode'], $result['itemname'], $result['pointrequired'], $result['value'], $result['itemtype'], $result['itemimage']);
        while ($pstmtselect->fetch()) {
            foreach ($result as $key => $value) {
                $row[$key] = $value;
            }
            $data[] = $row;
        }
        if ($data != null) {
            return $data;
        } else {
            return null;
        }
    }

    public function redeemItem() {
        $icno = trim($_POST['icno']);
        $itemcode = trim($_POST['itemcode']);
        $item = substr($itemcode, 0, 3);
        $s = strtoupper(md5(uniqid(rand(), true)));
        $guid = substr($s, 0, 8) . '-' . substr($s, 8, 4) . '-' . substr($s, 12, 4) . '-' . substr($s, 16, 4) . '-' . substr($s, 20);
        $pstmtinsert = $this->con->prepare($this->redeemitem);
        $pstmtinsert->bind_param('sss', $guid, $icno, $itemcode);
        $pstmtinsert->execute();
        if ($pstmtinsert->affected_rows > 0) {
            if ($item == 'CRD') {
                $value = $this->getItemValue($itemcode);
                require_once 'Member.php';
                $m = new Member();
                $m->updateBalance($value, $_SESSION['uid']);
                $pstmtupdate = $this->con->prepare('UPDATE redeemeditem SET status = "USED" WHERE referenceNo = ?;');
                $pstmtupdate->bind_param('s', $guid);
                $pstmtupdate->execute();
            }
            return $guid;
        }
        return null;
    }

    public function getItemValue($itemcode) {
        $pstmtselect = $this->con->prepare($this->getitemvalue);
        $pstmtselect->bind_param('s', $itemcode);
        $pstmtselect->bind_result($this->value);
        $pstmtselect->execute();
        if ($pstmtselect->fetch()) {
            return $this->value;
        }
        return null;
    }

    public function checkCoupon() {
        $result = array();
        $coupon = trim($_POST['coupon']);
        $pstmtselect = $this->con->prepare($this->checkcoupon);
        $pstmtselect->bind_param('s', $coupon);
        $pstmtselect->bind_result($result['referenceno'],$result['value'], $result['itemcode'], $result['status']);
        $pstmtselect->execute();
        if($pstmtselect->fetch()){
            foreach ($result as $key => $value) {
                $data[$key] = $value;
            }
            return $data;
        }
        return null;
    }

    public function getRedeemHistory() {
        $icno = trim($_POST['icno']);
        $result = array();
        $data = null;
        $pstmtselect = $this->con->prepare($this->getredeemhistory);
        $pstmtselect->bind_param('s', $icno);
        $pstmtselect->bind_result($result['referenceno'], $result['itemcode'], $result['redeemdate'], $result['status']);
        $pstmtselect->execute();
        while($pstmtselect->fetch()){
            foreach ($result as $key => $value) {
                $row[$key] = $value;
            }
            $data[] = $row;
        }
        return $data;
    }
    
    public function updateCouponStatus($referenceno){
        $pstmtupdate = $this->con->prepare($this->updatecouponstatus);
        $pstmtupdate->bind_param('s', $referenceno);
        $pstmtupdate->execute();
        if($pstmtupdate->affected_rows > 0){
            return true;
        }
        return false;
    }
}

?>
